<?php
defined('SYSPATH') OR die('No direct access allowed.');
/**
 * MAPI COA Project
 * product.php
 * @author ranuandra
 *
 * 22 Jul 2011
 * tags
 */

class Controller_Product extends Kohana_Mapi
{
	
	public function action_index()
	{
		$data = ORM::factory('product')->find_all();
		$view = View::factory('product/index');
		$view->datas = $data;
		$this->template->container = $view;
	}
	
	public function action_new()
	{
		$view = View::factory('product/form');
		$view->form = ORM::factory('product')->getForm();
		$this->template->container = $view;		
	}
	
	public function action_save()
	{
		if(isset($_POST))
		{

			try {
				//$post = Validation::factory($_POST);
					$product = ORM::factory('product');
					$product->create_product($_POST, $product->getField());
					Request::current()->redirect(URL::base().'product');

				}
				catch(ORM_Validation_Exception $e)
				{
					$errors = $e->errors('models');
					print_r($errors);
				}
			
		}
		else
		{
			Request::current()->redirect(URL::base().'product/new');	
		}		
	}
}